
	** Urban status
	
		use "$dir/rawdata/LSMS Tanzania/2020/hh_sec_a.dta", clear

		gen w5_urban=y5_rural==2 if !missing(y5_rural)
		keep y5_hhid w5_*
		
		tempfile urban
		save `urban', replace
		
	** Roster
	
		use "$dir/rawdata/LSMS Tanzania/2020/hh_sec_b.dta", clear
		
		gen w5_female=hh_b02==2 if !missing(hh_b02)
		gen w5_age=hh_b04
		
		gen w5_inhh=1
		
		gen w5_married=(hh_b19==1 | hh_b19==2) if !missing(hh_b19)
		
		bys y5_hhid: egen w5_hh_size=total(w5_inhh)
		
		keep y5_hhid indidy5 w5_*
		
		tempfile roster
		save `roster', replace
		
	** Employment 
	
		use "$dir/rawdata/LSMS Tanzania/2020/hh_sec_e1.dta", clear
	
		** Working for a wage outside the household
			gen w5_work_outsideHH=(hh_e03==1) if !missing(hh_e03)			
			gen w5_work_outsideHH_hrs=hh_e04
			replace w5_work_outsideHH_hrs=0 if w5_work_outsideHH==0
			
				gen test=hh_e41
				replace test=test+hh_e54 if !missing(hh_e54)
				gen diff=test-w5_work_outsideHH_hrs
				gen diff10=diff>=(-10 & diff<=10) if !missing(diff)
				compare w5_work_outsideHH_hrs test
				drop test				
			
		** Working in agriculture for a HH member
			gen w5_work_agHH=hh_e07==1 if !missing(hh_e07)
			gen w5_work_agHH_hrs=hh_e08
			
		** Working in HH business
			gen w5_work_bizHH=(hh_e05==1 | hh_e06a==1) if (!missing(hh_e05) | !missing(hh_e06a))
			gen w5_work_bizHH_hrs=hh_e06
			replace w5_work_bizHH_hrs=w5_work_bizHH_hrs+hh_e06b if !missing(hh_e06b)
			
		** Any work in last 7 days (agriculture, for non-HH member, own account/business)
			egen w5_worked_last7days=rowmax(w5_work_outsideHH w5_work_agHH w5_work_bizHH)
		
		** Hours worked in last 7 days
			egen w5_work_hrs=rowtotal(w5_work_outsideHH_hrs w5_work_agHH_hrs w5_work_bizHH_hrs)
			replace w5_work_hrs=. if missing(w5_work_outsideHH_hrs) & missing(w5_work_agHH_hrs) & missing(w5_work_bizHH_hrs)
		
		keep y5_hhid indidy5 w5_*
		
		tempfile employment
		save `employment', replace
		
	** Illness and injury
		
		*n/a

	** Disabilities

		use "$dir/rawdata/LSMS Tanzania/2020/hh_sec_d.dta", clear

		** Difficulty seeing
		tab1 hh_d17
		gen w5_diff_seei=1 if inlist(hh_d17, 3, 4, 5) 
		replace w5_diff_seei=0 if inlist(hh_d17, 1, 2)
		replace w5_diff_seei=. if inlist(hh_d17, .)
		tab1 w5_diff_seei

		** Difficulty hearing
		tab1 hh_d19
		gen w5_diff_hear=1 if inlist(hh_d19, 3, 4, 5) 
		replace w5_diff_hear=0 if inlist(hh_d19, 1, 2)
		replace w5_diff_hear=. if inlist(hh_d19, .)
		tab1 w5_diff_hear

		** Difficulty walking or climbing stairs	
		tab1 hh_d21
		gen w5_diff_walk_clim=1 if inlist(hh_d21, 3, 4, 5) 
		replace w5_diff_walk_clim=0 if inlist(hh_d21, 1, 2)
		replace w5_diff_walk_clim=. if inlist(hh_d21, .)
		tab1 w5_diff_walk_clim

		** Difficulty remembering or concentrating
		tab1 hh_d23
		gen w5_diff_reme=1 if inlist(hh_d23, 3, 4, 5) 
		replace w5_diff_reme=0 if inlist(hh_d23, 1, 2)
		replace w5_diff_reme=. if inlist(hh_d23, .)
		tab1 w5_diff_reme

		** Difficulty with self-care
		tab1 hh_d25
		gen w5_diff_self_care=1 if inlist(hh_d25, 3, 4, 5) 
		replace w5_diff_self_care=0 if inlist(hh_d25, 1, 2)
		replace w5_diff_self_care=. if inlist(hh_d25, .)
		tab1 w5_diff_self_care

		** Difficulty communicating
		tab1 hh_d27
		gen w5_diff_comm=1 if inlist(hh_d27, 3, 4, 5) 
		replace w5_diff_comm=0 if inlist(hh_d27, 1, 2)
		replace w5_diff_comm=. if inlist(hh_d27, .)
		tab1 w5_diff_comm

		keep y5_hhid indidy5 w5_*
	    tempfile disabilities
		save `disabilities', replace
		
		
	** Subjective well-being
	
		use "$dir/rawdata/LSMS Tanzania/2020/hh_sec_g.dta", clear

			forvalues x=1/8 {
				if `x'==1 local desc "health"
				if `x'==2 local desc "finances"
				if `x'==3 local desc "housing"
				if `x'==4 local desc "job"
				if `x'==5 local desc "healthcare"
				if `x'==6 local desc "educ"
				if `x'==7 local desc "safety"
				if `x'==8 local desc "life"
				gen w5_`desc'_vsatisfied=hh_g03_`x'==1 if !missing(hh_g03_`x') & hh_g03_`x'!=8
				gen w5_`desc'_satisfied=(hh_g03_`x'==1 | hh_g03_`x'==2 | hh_g03_`x'==3) if !missing(hh_g03_`x') & hh_g03_`x'!=8
				gen w5_`desc'_dissatisfied=(hh_g03_`x'==5 | hh_g03_`x'==6 | hh_g03_`x'==7) if !missing(hh_g03_`x') & hh_g03_`x'!=8
			}
			
			gen w5_finstatus_rich_comf=(hh_g04==1 | hh_g04==2 | hh_g04==3) if !missing(hh_g04) & hh_g04!=8
			gen w5_finstatus_cangetby=(hh_g04==4) if !missing(hh_g04) & hh_g04!=8
			gen w5_finstatus_neverenough=(hh_g04==5 | hh_g04==6) if !missing(hh_g04) & hh_g04!=8
			gen w5_finstatus_poordestitute=(hh_g04==7) if !missing(hh_g04) & hh_g04!=8
		 
		keep y5_hhid indidy5 w5*
		
		tempfile subjective
		save `subjective', replace
	
	** Merge and save
	
		use `roster', clear
		merge 1:1 y5_hhid indidy5 using `employment', gen(merge_employment)
		merge 1:1 y5_hhid indidy5 using `subjective', gen(merge_subjective)
		merge 1:1 y5_hhid indidy5 using `disabilities', gen(merge_disabilities)
		merge m:1 y5_hhid using `urban', gen(merge_urban)
		
		ren y5_hhid hhid_w5
		ren indidy5 indiv_w5
		
		save "$dir/data/tza_w5_individual.dta", replace
